<?php
declare (strict_types=1);

namespace app\common\listener;

use app\common\model\OrderProduct;
use app\common\model\Product;
use app\common\model\ProductSpec;

class Operation
{
    public function handle($data): void
    {
        $list = OrderProduct::where(['order_id' => $data])->field('pid,spec,num')->select();

        foreach ($list as $item) {
            Product::where(['id' => $item->pid])
                ->dec('sales', floatval($item->num))
                ->inc('stock', floatval($item->num))
                ->save();
            if ($item->spec) {
                ProductSpec::where(['pid' => $item->pid, 'merge_title' => $item->spec])->inc('stock', floatval($item->num))->save();
            }
        }
    }
}